<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 15:21
 */
namespace app\business\controller;

use think\Controller;
use think\Db;
use think\Session;

class Common extends Controller
{
	/**
	 * 查看是否登录
	*/
	public function _initialize()
	{
		if(!Session::has('business')){
			$this->redirect('business/login/index');
		}
		//获取头像
		$image = Db::table('business')->where('id',Session::get('business.id'))->value('logo');
		$this->assign('image',$image);
	}
	
	/**
	 * 更换头像
	 * @param array request()->file 头像图片
	*/
	public function updatelogo()
	{
		if(request()->isPost()){
			try{
				if(request()->file('file')){
					$image = request()->file('file');
					$imagePath = ROOT_PATH .DS.'public'.DS.'business'.DS.'logo';
					$info = $image->validate(['size'=>500000,'ext'=>'jpg,png,gif,jpeg'])->move($imagePath);
					if($info){
						$input['logo'] = 'business'.DS.'logo'.DS.$info->getSaveName();
						$result = Db::table('business')->where('id',Session::get('business.id'))->update($input);
						if(!$result){
							throw new \PDOException('头像更新失败');
						}
						Session::set('business.logo',$input['logo']);
						$success = ['image'=>$input['logo']];
						return json($success);
					}else{
						throw new \PDOException($image->getError());
					}
				}else{
					throw new \PDOException('请选择图片');
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 修改个人信息
	*/
	public function edit()
	{
		return view('edit');
	}
	/**
	 * 执行修改
	 * @param string post.name 昵称
	 * @param string post.linkman 联系人
	 * @param string post.telephone 联系电话
	 * @param string post.paymentaccount 收款账号
	 * @param string post.bank 所属银行
	 * @param string post.branch 支行
	*/
	public function update()
	{
		if(request()->isAjax()){
			try{
				$input['name'] = input('post.nickname');
				$name = Db::table('business')->where('name',$input['name'])->value('name');
				if($name){
					throw new \PDOException('昵称已存在');
				}
				$input['linkman'] = input('post.linkman');
				$input['telephone'] = input('post.telephone');
				$input['paymentaccount'] = input('post.paymentaccount');
				$input['bank'] = input('post.bank');
				$input['branch'] = input('post.branch');
				$result = Db::table('business')->where('id',Session::get('business.id'))->update($input);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				Session::set('business.name',$input['name']);
				Session::set('business.linkman',$input['linkman']);
				Session::set('business.telephone',$input['telephone']);
				Session::set('business.paymentaccount',$input['paymentaccount']);
				Session::set('business.bank',$input['bank']);
				Session::set('business.branch',$input['branch']);
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 修改密码
	*/
	public function pass()
	{
		return view('pass');
	}
	
	/**
	 * 更新密码
	 * @param string post.pass 原密码
	 * @param string post.password 新密码
	*/
	public function updatepass()
	{
		if(request()->isAjax()){
			try{
				$pass = input('post.pass');
				$password = input('post.password');
				if(encrypt($pass) != Session::get('business.password')){
					throw new \PDOException('原密码错误');
				}
				$input['password'] = encrypt($password);
				$result = Db::table('business')->where('id',Session::get('business.id'))->update($input);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				Session::delete('business');
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 城市级联操作
	 */
	public function find($id=0){
		
		$data = Db::table('District')->where("upid={$id}")->select();
		return json($data);
	}
}